package com.dayatang.rule.examples;

import java.io.InputStreamReader;
import java.io.Reader;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.StatefulSession;
import org.drools.audit.WorkingMemoryFileLogger;
import org.drools.compiler.PackageBuilder;
import org.drools.event.DebugAgendaEventListener;
import org.drools.event.DebugWorkingMemoryEventListener;
import org.drools.rule.Package;

public class HelloWorld {

	public static final void main(final String[] args) throws Exception {
		// read in the source
		final Reader source = new InputStreamReader(HelloWorld.class
				.getResourceAsStream("/rule/HelloWorld.drl"));

		final PackageBuilder builder = new PackageBuilder();

		// this wil parse and compile in one step
		builder.addPackageFromDrl(source);

		// Check the builder for errors
		if (builder.hasErrors()) {
			System.out.println(builder.getErrors().toString());
			throw new RuntimeException("Unable to compile \"HelloWorld.drl\".");
		}

		// get the compiled package (which is serializable)
		final Package pkg = builder.getPackage();

		// add the package to a rulebase (deploy the rule package).
		final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
		ruleBase.addPackage(pkg);

		final StatefulSession session = ruleBase.newStatefulSession();

		session.addEventListener(new DebugAgendaEventListener());
		session.addEventListener(new DebugWorkingMemoryEventListener());

		final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger(
				session);
		logger.setFileName("log/helloworld");

		final Message message = new Message();
		message.setMessage("Hello World");
		message.setStatus(Message.HELLO);
		session.insert(message);

		session.fireAllRules();

		logger.writeToDisk();

		session.dispose();
	}

	public static class Message {
		public static final int HELLO = 0;
		public static final int GOODBYE = 1;

		private String message;

		private int status;

		public Message() {

		}

		public String getMessage() {
			return this.message;
		}

		public void setMessage(final String message) {
			this.message = message;
		}

		public int getStatus() {
			return this.status;
		}

		public void setStatus(final int status) {
			this.status = status;
		}
	}
}
